package com.zwh.modules.scheduler.service;

import com.zwh.modules.scheduler.job.ProjectStatusUpdateJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhangwenhe
 */
@Service
public class SchedulerJobService {
    @Autowired
    @Qualifier(value = "schedulerFactoryBean")
    private Scheduler scheduler;



    public void startJob() throws SchedulerException{
        initJob(scheduler);
//        projectStatusUpdateJob(scheduler);

        scheduler.start();
    }

    private void initJob(Scheduler scheduler) throws SchedulerException {
        // 更新项目状态定时任务
//        this.addJob(scheduler, ProjectStatusUpdateJob.class,"projectStatusUpdateJob","group",
//                "0/5 * * * * ?");
        // 任务到期提醒通知
//        this.addJob(scheduler, ProjectTaskNotifyJob.class,"projectTaskNotifyJob","group",
//                "0 0 14 * * ?");
        // 任务过期提醒通知
//        this.addJob(scheduler, ProjectTaskTimeoutNotifyJob.class,"projectTaskTimeoutNotifyJob","group",
//                "0 0 14 * * ?");
//        this.addJob(scheduler, ProjectTaskTimeoutNotifyJob.class,"projectTaskTimeoutNotifyJob","group",
//                "30 40 * * * ?");
    }

    /**
     * 添加实现 org.quartz.Job接口的job定时任务类
     *
     * @param scheduler
     * @param name 定时任务的名称
     * @param group group 定时任务组名称
     * @param cronExpression 定时任务时间表达式"0 0 14 * * ?"
     * @throws SchedulerException
     */
    private void addJob(Scheduler scheduler,Class<? extends Job> jobClass,String name,String group,String cronExpression) throws SchedulerException {
        // 通过JobBuilder构建JobDetail实例，JobDetail规定只能是实现Job接口的实例
        // JobDetail 是具体Job实例
        JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(name, group).build();
        // 基于表达式构建触发器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
        // CronTrigger表达式触发器 继承于Trigger
        // TriggerBuilder 用于构建触发器实例
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                .withSchedule(cronScheduleBuilder).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    /**
     * 暂停所有任务
     *
     * @throws SchedulerException
     */
    public void addJob(JobDetail jobDetail,Trigger trigger) throws SchedulerException {
        scheduler.scheduleJob(jobDetail,trigger);
    }

    /**
     * 获取Job信息
     *
     * @param name
     * @param group
     * @return
     * @throws SchedulerException
     */
    public String getJobInfo(String name, String group) throws SchedulerException {
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        return String.format("time:%s,state:%s", cronTrigger.getCronExpression(),
                scheduler.getTriggerState(triggerKey).name());
    }

    /**
     * 修改某个任务的执行时间
     *
     * @param name
     * @param group
     * @param time
     * @return
     * @throws SchedulerException
     */
    public boolean modifyJob(String name, String group, String time) throws SchedulerException {
        Date date = null;
        TriggerKey triggerKey = new TriggerKey(name, group);
        CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        String oldTime = cronTrigger.getCronExpression();
        if (!oldTime.equalsIgnoreCase(time)) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(time);
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group)
                    .withSchedule(cronScheduleBuilder).build();
            date = scheduler.rescheduleJob(triggerKey, trigger);
        }
        return date != null;
    }

    /**
     * 暂停所有任务
     *
     * @throws SchedulerException
     */
    public void pauseAllJob() throws SchedulerException {
        scheduler.pauseAll();
    }

    /**
     * 暂停某个任务
     *
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void pauseJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.pauseJob(jobKey);
    }

    /**
     * 恢复所有任务
     *
     * @throws SchedulerException
     */
    public void resumeAllJob() throws SchedulerException {
        scheduler.resumeAll();
    }

    /**
     * 恢复某个任务
     *
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void resumeJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.resumeJob(jobKey);
    }

    /**
     * 删除某个任务
     *
     * @param name
     * @param group
     * @throws SchedulerException
     */
    public void deleteJob(String name, String group) throws SchedulerException {
        JobKey jobKey = new JobKey(name, group);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return;
        }
        scheduler.deleteJob(jobKey);
    }
}
